//header上的按钮
(function () {
    //菜单按钮
    var $left_panel = $('#left_panel');
    var $body = $('body');
    var $toggleMenu_btn = $('#page_header .toggleMenu_btn');
    $toggleMenu_btn.on('click', function () {
        $body.removeClass('minified');
        if (parseInt($left_panel.css('left')) == 0) {
            $left_panel.css('left', 220);
            $toggleMenu_btn.addClass('active');
        } else {
            $left_panel.css('left', 0);
            $toggleMenu_btn.removeClass('active');
        }
    });
    $(window).on('resize', function () {
        if ($(window).width() >= 768) {
            $left_panel.css('left', 0);
            $toggleMenu_btn.removeClass('active');
        } else {
            $body.removeClass('minified');
        }
    });
})();

//主导航
(function () {
    var $nav = $('#left_panel nav');
    var $a = $nav.find('a');
    $a.each(function () {
        var $btn = $(this);
        var $parent_li = $btn.parent();
        var $ul = $btn.next('ul');
        var $i = $btn.find('b i');
        if ($ul.length >= 1) {
            $btn.on('click', function () {
                var ul_orgH = $ul.innerHeight();
                if (!$parent_li.hasClass('open')) {
                    $parent_li.addClass('open');
                    $ul.css({'height': 0}).css('display', 'block');
                    $i.removeClass('fa-plus-square-o').addClass('fa-minus-square-o');
                    $ul.animate({"height": ul_orgH}, {
                            queue: false, duration: 200, complete: function () {
                                $ul.css('height', 'auto');
                            }
                        }
                    );
                } else {
                    $parent_li.removeClass('open');
                    $i.removeClass('fa-minus-square-o').addClass('fa-plus-square-o');
                    $ul.animate({"height": 0}, {
                            queue: false, duration: 200, complete: function () {
                                $ul.css({'height': "auto"}).css('display', 'none');
                            }
                        }
                    );
                }

                $others_li = $btn.parent().siblings('.open');
                $others_li.each(function () {
                    var $parent_li = $(this);
                    var $btn = $parent_li.find('a:first');
                    var $ul = $btn.next('ul');
                    var $i = $btn.find('b i');
                    $parent_li.removeClass('open');
                    $i.removeClass('fa-minus-square-o').addClass('fa-plus-square-o');
                    $ul.animate({"height": 0}, {
                            queue: false, duration: 200, complete: function () {
                                $ul.css({'height': "auto"}).css('display', 'none');
                            }
                        }
                    );
                });
            });
        }
    });

    var $minifyBtn = $('#left_panel .minifyBtn');
    var $body = $('body');
    $minifyBtn.on('click', function () {
        $('#left_panel nav li.open').each(function () {
            var $parent_li = $(this);
            var $btn = $parent_li.find('a:first');
            var $ul = $btn.next('ul');
            var $i = $btn.find('b i');
            $parent_li.removeClass('open');
            $i.removeClass('fa-minus-square-o').addClass('fa-plus-square-o');
            $ul.css({'height': "auto"}).css('display', 'none');
        });
        if (!$body.hasClass('minified')) {
            $body.addClass('minified');
            $(this).find('i').addClass('fa-flip-horizontal');
        } else {
            $body.removeClass('minified');
            $(this).find('i').removeClass('fa-flip-horizontal');
        }
    });
})();
